package Tree;

import java.util.Arrays;
import java.util.Scanner;

/**
 * @author chenhong
 * @version 1.0
 * @description TODO
 * @date 2023/4/20 11:22
 */

class Node {
    int val;
    Node left;
    Node right;

    Node(int val) {
        this.val = val;
    }
}

public class CountFullTree2 {
    int count = 0;
    int[] depth;
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        Node[] nodes = new Node[n + 1];
        for (int i = 1; i <= n; i++) {
            nodes[i] = new Node(i);
        }
        for (int i = 1; i <= n; i++) {
            int left = sc.nextInt();
            int right = sc.nextInt();
            if (left != -1) nodes[i].left = nodes[left];
            if (right != -1) nodes[i].right = nodes[right];
        }
        CountFullTree2 solution = new CountFullTree2();
        Arrays.fill(solution.depth, -1);
        solution.isFullBinaryTree(nodes[1]);
        System.out.println(solution.count);
    }

    public boolean isFullBinaryTree(Node root) {
        if (root == null) return true;
        boolean leftFull = isFullBinaryTree(root.left);
        boolean rightFull = isFullBinaryTree(root.right);
        if (leftFull && rightFull && getDepth(root.left) == getDepth(root.right)) {
            count++;
            return true;
        }
        return false;
    }

    public int getDepth(Node root) {
        if (root == null) return 0;
        if (depth[root.val] != -1) return depth[root.val];
        depth[root.val] = Math.max(getDepth(root.left), getDepth(root.right)) + 1;
        return depth[root.val];
    }
}

